package com.example.stream;

import com.example.Employee;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

import com.example.Employee.Status;

//008
/*
 * 终止操作：查找与匹配
 */
public class TestStreamAPI3 {

    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99, Status.FREE),
            new Employee("李四", 38, 5555.99, Status.BUSY),
            new Employee("王五", 50, 6666.66, Status.VOCATION),
            new Employee("赵六", 16, 3333.33, Status.FREE),
            new Employee("田七", 8, 7777.77, Status.BUSY),
            new Employee("田七", 8, 7777.77, Status.BUSY)
    );

    /*
     * 查找与匹配
     * allMatch--检查是否匹配所有元素
     * anyMatch--检查是否至少匹配一个元素
     * noneMatch--检查是否没有匹配的所有元素
     * findFirst--返回第一个元素
     * findAny--返回当前流中的任意元素
     * count--返回流中元素的总个数
     * max--返回流中最大值
     * min--返回流中最小值
     */

    @Test
    public void test1() {
        boolean b1 = employees.stream()
                .allMatch((e) -> e.getStatus().equals(Status.BUSY));
        System.out.println(b1);

        boolean b2 = employees.stream()
                .anyMatch((e) -> e.getStatus().equals(Status.BUSY));
        System.out.println(b2);

        boolean b3 = employees.stream()
                .noneMatch((e) -> e.getStatus().equals(Status.BUSY));
        System.out.println(b3);

        Optional<Employee> first = employees.stream()
                .sorted((emp1, emp2) -> -Double.compare(emp1.getSalary(), emp2.getSalary()))
                .findFirst();
        System.out.println(first.get());

        Optional<Employee> any = employees.parallelStream()
                .filter((emp) -> emp.getStatus().equals(Status.FREE))
                .findAny();
        System.out.println(any.get());
    }

    @Test
    public void test2() {
        long count = employees.stream()
                .count();
        System.out.println(count);

        Optional<Employee> max = employees.stream()
                .max((emp1, emp2) -> Double.compare(emp1.getSalary(), emp2.getSalary()));
        System.out.println(max.get());

        Optional<Double> min = employees.stream()
                .map((emp) -> emp.getSalary())
                .min(Double::compareTo);

        System.out.println(min.get());
    }

    /*
     * 归约：
     * reduce(T identity, BinaryOperator) / reduce(BinaryOperator)
     * --可以将流中元素反复结合起来，得到一个值
     */

    @Test
    public void test3() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        Integer reduce = list.stream()
                .reduce(0, (x, y) -> x + y);
        System.out.println(reduce);

        Optional<Double> reduce1 = employees.stream()
                .map((emp) -> emp.getSalary())
                .reduce(Double::sum);
        System.out.println(reduce1.get());
    }

    /*
     * 收集：
     * collect--将流转换成其他形式。接收一个Collector接口的实现，用于给Stream中元素做汇总的方法。
     */
    @Test
    public void test4() {
        List<String> collect = employees.stream()
                .map(Employee::getName)
                .collect(Collectors.toList());
        collect.forEach(System.out::println);
        System.out.println("--------------");
        Set<String> collect1 = employees.stream()
                .map(Employee::getName)
                .collect(Collectors.toSet());
        collect1.forEach(System.out::println);
        System.out.println("--------------");
        HashSet<String> collect2 = employees.stream()
                .map(Employee::getName)
                .collect(Collectors.toCollection(HashSet::new));
        collect2.forEach(System.out::println);
    }

    @Test
    public void test5() {
        Long count = employees.stream()
                .collect(Collectors.counting());
        System.out.println(count);

        Double avg = employees.stream()
                .collect(Collectors.averagingDouble(Employee::getSalary));

        System.out.println(avg);

        Double sum = employees.stream()
                .collect(Collectors.summingDouble(Employee::getSalary));
        System.out.println(sum);

        Optional<Employee> maxInfo = employees.stream()
                .collect(Collectors.maxBy((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())));
        System.out.println(maxInfo.get());

        Optional<Double> min = employees.stream()
                .map(Employee::getSalary)
                .collect(Collectors.minBy(Double::compareTo));
        System.out.println(min.get());
    }

    //分组
    @Test
    public void test6() {
        Map<Status, List<Employee>> group = employees.stream()
                .collect(Collectors.groupingBy(Employee::getStatus));
        System.out.println(group);
    }

    //多级分组
    @Test
    public void test7() {
        Map<Status, Map<String, List<Employee>>> collect = employees.stream()
                .collect(Collectors.groupingBy(Employee::getStatus,
                        Collectors.groupingBy((emp) -> {
                            if (emp.getAge() <= 35) {
                                return "青年";
                            } else if (emp.getAge() <= 50) {
                                return "中年";
                            } else {
                                return "老年";
                            }
                        })));
        System.out.println(collect);
    }

    //分片
    @Test
    public void test8() {
        Map<Boolean, List<Employee>> collect = employees.stream()
                .collect(Collectors.partitioningBy((emp) -> emp.getSalary() > 8000));
        System.out.println(collect);
    }

    @Test
    public void test9() {
        DoubleSummaryStatistics collect = employees.stream()
                .collect(Collectors.summarizingDouble(Employee::getSalary));
        System.out.println(collect.getMax());
        System.out.println(collect.getMin());
        System.out.println(collect.getAverage());
    }

    @Test
    public void test10() {
        String str1 = employees.stream()
                .map(Employee::getName)
                .collect(Collectors.joining());
        System.out.println(str1);


        String str2 = employees.stream()
                .map(Employee::getName)
                .collect(Collectors.joining(","));
        System.out.println(str2);


        String str3 = employees.stream()
                .map(Employee::getName)
                .collect(Collectors.joining(",", "#", "#"));
        System.out.println(str3);
    }
}
